package vivace.view;

import javax.swing.*;

import com.sun.org.apache.xerces.internal.xs.ItemPSVI;

import java.awt.event.*;
import java.io.File;
import java.util.*;

import vivace.model.*;
import vivace.controller.*;
import vivace.view.openDialogue.*;
import lib.Komponenter.*;

public class AppMenuBar extends JMenuBar implements Observer {

	private App model;
	private AppController controller;

	private JMenu fileMenu,preferencesMenu,windowsMenu, toolsMenu;
	private JMenuItem itemNew, itemOpen, itemSave, itemSaveAs, itemSaveAll, itemClose, itemCloseAll, itemQuit, itemChooseDevice, itemMidiPanic;
	
	private JFileChooser fileChooser;
	private String currentDevice;
	
	public AppMenuBar( App model ){
		this.model = model;
		this.model.addObserver(this);
		this.controller = new AppController(model);
		initialize();
	}
	
	private void initialize(){
		
		// Initialize the main menu

        // Create the File menu
        fileMenu = new CMenu("File");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        
        // Create the Window menu 
        windowsMenu = new CMenu("WINDOW");
        windowsMenu.setMnemonic(KeyEvent.VK_W);

		// Create the Preferences menu
		preferencesMenu = new CMenu("MIDI_PREFS");
		preferencesMenu.setMnemonic(KeyEvent.VK_P);
		
        // Create the File/New item
        itemNew = new CMenuItem("New");
        itemNew.setMnemonic(KeyEvent.VK_N);
        itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK));
        itemNew.setActionCommand("new");
        itemNew.addActionListener( this.controller );
        fileMenu.add(itemNew);
        
        // Create the File/Open item
        itemOpen = new CMenuItem("Open");
        itemOpen.setActionCommand("open");
        itemOpen.addActionListener( this.controller );
        fileMenu.add(itemOpen);

        // Create the File/Save item
        itemSave = new CMenuItem("SAVE");
        itemSave.setMnemonic(KeyEvent.VK_S);
        itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.ALT_MASK));
        itemSave.setActionCommand("save");
        itemSave.addActionListener( this.controller );
        fileMenu.add(itemSave);

        // Create the File/Save as item
        itemSaveAs = new CMenuItem("SAVE_AS");
        itemSaveAs.setActionCommand("saveAs");
        itemSaveAs.addActionListener( this.controller );
        fileMenu.add(itemSaveAs);

        // Create the File/Save all item
        itemSaveAll = new CMenuItem("SAVE_ALL");
        itemSaveAll.setActionCommand("saveAll");
        itemSaveAll.addActionListener( this.controller );
        fileMenu.add(itemSaveAll);

        // Create the File/Close project item
        itemClose = new CMenuItem("CLOSE");
        itemClose.setActionCommand("close");
        itemClose.addActionListener( this.controller );
        fileMenu.add(itemClose);
        
        // Create the File/Close project item
        itemCloseAll = new CMenuItem("CLOSE_ALL");
        itemCloseAll.setActionCommand("closeAll");
        itemCloseAll.addActionListener( this.controller );
        fileMenu.add(itemCloseAll);
        
        // Create the File/Quit item
        itemQuit = new CMenuItem("QUIT");
        itemQuit.setMnemonic(KeyEvent.VK_Q);
        itemQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.ALT_MASK));
        itemQuit.setActionCommand("quit");
        itemQuit.addActionListener( this.controller );
        fileMenu.add(itemQuit);
	
		// Create the Preferences/Choose device item
		itemChooseDevice = new CMenuItem("CH_DEVICE");
		itemChooseDevice.setActionCommand("chooseDevice");
		itemChooseDevice.addActionListener( this.controller );
		preferencesMenu.add(itemChooseDevice);
		
		/* Create tools menu */
		toolsMenu = new CMenu("TOOLS");
		
		// create "Panic!" item
		itemMidiPanic = new CMenuItem("PANIC");
		itemMidiPanic.setActionCommand("panic");
		itemMidiPanic.addActionListener(controller);
		toolsMenu.add(itemMidiPanic);
		
		// Add the menus to the menu bar
        this.add(fileMenu);
        this.add(preferencesMenu);
        this.add(toolsMenu);
        this.add(windowsMenu);
        
        // Paint the menu
        repaintMenu();
	}

	@Override
	public void update(Observable arg0, Object arg1) {

		// Repaint the menu
		repaintMenu();
		
	}

	private void repaintMenu(){

		// Handle enabling/disabling of menu items
		boolean enable = !(this.model.getActiveProject() == null);
		this.windowsMenu.setEnabled(enable);
		this.itemSave.setEnabled(enable && !this.model.getActiveProject().getIsSaved());
		this.itemSaveAll.setEnabled(enable);
		this.itemSaveAs.setEnabled(enable);
		this.itemClose.setEnabled(enable);
		this.itemCloseAll.setEnabled(enable);
		this.itemMidiPanic.setEnabled(enable);
					
		// If enabled, repaint the window menu
		if( enable ){
			this.windowsMenu.removeAll();
	
			// For each open project, create a menu item and mark the one that is set to active
			Iterator<Map.Entry<Integer, Project>> ps = this.model.getProjectsIterator();
			JMenuItem menuItem;
			Map.Entry<Integer, Project> e;
			Project p;
			Integer k;
			while(ps.hasNext()){
				e = ps.next();
				k = e.getKey();
				p = e.getValue();
				menuItem = new JMenuItem();
				menuItem.setText(p.getName() + (k.equals(this.model.getActiveProjectKey()) ? " (x)" : ""));
				menuItem.setActionCommand("setActiveProject_" + k.toString());
				menuItem.addActionListener( this.controller );
				this.windowsMenu.add(menuItem);
			}
		}
	}
	
}
